package tp.pr3;

public class Door
{

	private Room origen;
	private Room destino;
	private Directions direccion;
	private boolean bidi;
	private boolean abierta;

	// constructors
	public Door(Room source, Directions direction, Room target,
			boolean bidirectional)
	{
		this.origen = source;
		this.direccion = direction;
		this.destino = target;
		this.bidi = bidirectional;
		this.abierta = false;
	}

	public Door(Room source, Directions direction, Room target,
			boolean bidirectional, boolean isOpen)
	{
		this.origen = source;
		this.direccion = direction;
		this.destino = target;
		this.bidi = bidirectional;
		this.abierta = isOpen;
	}

	// methods

	public boolean isInRoom(Room room)
	{
		boolean esta = false;

		if (!this.bidi && room == this.origen)
		{
			esta = true;
		}
		else
		{
			if ((room == this.destino || room == this.origen) && this.bidi)
				esta = true;
		}

		return esta;
	}

	public boolean isInRoom(Room room, Directions where) 
	{
		boolean pertenece = false;

		if ((room == this.origen && where == this.direccion)
				|| (room == this.destino && where == this.direccion.getOpuesto()))
		{
			pertenece = true;
		}

		return pertenece;
	}

	public void open()
	{
		this.abierta = true;
	}

	public void close()
	{
		this.abierta = false;
	}

	public Room nextRoom(Room whereAmI)
	{
		Room siguiente = null;

		if (whereAmI == this.origen)
		{
			siguiente = this.destino;
		}

		if (whereAmI == this.destino && bidi) // para las bidireccionales
		{
			siguiente = this.origen;
		}

		return siguiente;
	}

	public boolean connect(Room roomA, Room roomB) // dudas
	{
		boolean conectado = false;
		if (!this.bidi && roomA == this.origen && roomB == this.destino
				&& this.isOpen())
		{
			conectado = true;
		}
		else if (this.bidi && this.isOpen())
		{
			if ((roomA == this.origen || roomA == this.destino)
					&& (roomB == this.origen || roomB == this.destino))
			{
				conectado = true;
			}
		}

		return conectado;
	}

	public boolean isOpen()
	{
		return this.abierta;
	}

	// getters necesarios para la clase map y dem�s

	public Room getOrigen()
	{
		return this.origen;
	}

	public Room getDestino()
	{
		return this.destino;
	}

	public Directions getDir()
	{
		return this.direccion;
	}

	public boolean getBidi()
	{
		return this.bidi;
	}
}
